package io.gitee.yth.design.pattern.test.core.pattern.creational.factory;

import io.gitee.yth.design.pattern.test.core.pattern.creational.factory.pizza.Pizza;
import io.gitee.yth.design.pattern.test.core.pattern.creational.factory.pizzaorderservice.ChicagoPizzaOrderService;
import io.gitee.yth.design.pattern.test.core.pattern.creational.factory.pizzaorderservice.IPizzaOrderService;

import java.io.InputStream;
import java.util.Scanner;

/**
 * @author yth
 * @since 2023-04-17-0:07
 */
public class PizzaStore {

    private IPizzaOrderService service;
    private Scanner in;

    public void setService(IPizzaOrderService service) {
        this.service = service;
    }

    public void setSource(InputStream source) {
        in = new Scanner(source);
    }

    public void open() {
        while (true) {
            System.out.println("Please input the pizza type ...");
            String orderType = getOrder();
            if (orderType == null) break;
            Pizza pizza = service.orderPizza(orderType);
            if (pizza == null) {
                System.out.println("sorry, we don't sell this kind of pizza.");
            } else {
                System.out.println("Here you are: " + pizza);
            }
        }
    }

    private String getOrder() {
        return in.hasNextLine() ? in.nextLine().trim() : null;
    }

    public static void main(String[] args) {
        PizzaStore shop = new PizzaStore();
        shop.setService(new ChicagoPizzaOrderService());
        shop.setSource(System.in);
        shop.open();
    }

}
